package dao;

import java.util.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import bean.Log;
import bean.User;
import utils.DBCPUtil;

public class LogManagerDaoImpl implements LogManagerDao {
	QueryRunner qr=new QueryRunner(DBCPUtil.getDataSource());
	/**
	 * 将日志加入到数据库
	 * @param log
	 * @return
	 */
	@Override
	public boolean addLog(Log log) {
		// TODO Auto-generated method stub
		try {
			qr.update("insert into log(excutor,excution,modify_time,state)values(?,?,?,?)", log.getExcutor(),log.getExcution(),new Date(),log.isState());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
		return true;
	}
	/**
	 * 得到数据库中第st行到en行的日志数据，用于分页展示
	 * @param st
	 * @param en
	 * @return
	 */
	@Override
	public List<Log> getPageLog(int st, int en) {
		// TODO Auto-generated method stub
		List<Log> logs=new ArrayList<Log>();
		try {
			logs=qr.query("select * from log limit ?,?", new BeanListHandler<>(Log.class),st-1,en-st+1);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return logs;
	}
		
	/**
	 * 得到所有日志内容
	 * @return
	 */
	@Override
	public List<Log> getLog() {
		// TODO Auto-generated method stub
		List<Log> logs=new ArrayList<Log>(); 
		 try {
			logs = qr.query("select * from log", new BeanListHandler<>(Log.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		//logs=(List<Log>) qr.query("select * from log",new BeanHandler<>(Log.class));
		return logs;
	}
	@Override
	public List<Log> getLogByExcuter(String excuter) {
		// TODO Auto-generated method stub
		List<Log> logs=new ArrayList<Log>(); 
		 try {
			logs = qr.query("select * from log where excutor=?", new BeanListHandler<>(Log.class),excuter);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		//logs=(List<Log>) qr.query("select * from log",new BeanHandler<>(Log.class));
		return logs;
	}
	@Override
	public List<Log> getLogByExcution(String excution) {
		// TODO Auto-generated method stub
		List<Log> logs=new ArrayList<Log>(); 
		 try {
			logs = qr.query("select * from log where excution=?", new BeanListHandler<>(Log.class),excution);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		//logs=(List<Log>) qr.query("select * from log",new BeanHandler<>(Log.class));
		return logs;
	}
	@Override
	public List<Log> getLogByTime(Date date) {
		// TODO Auto-generated method stub
		List<Log> logs=new ArrayList<Log>(); 
		 try {
			logs = qr.query("select * from log where modify_time=?", new BeanListHandler<>(Log.class),date);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		//logs=(List<Log>) qr.query("select * from log",new BeanHandler<>(Log.class));
		return logs;
	}

	@Override
	public List<Log> getLogByState(int state) {
		// TODO Auto-generated method stub
		List<Log> logs=new ArrayList<Log>(); 
		 try {
			logs = qr.query("select * from log where state=?", new BeanListHandler<>(Log.class),state);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		//logs=(List<Log>) qr.query("select * from log",new BeanHandler<>(Log.class));
		return logs;
	}
	/**
	 * 删除对应id的日志
	 * @param log_id
	 * @return
	 */
	@Override
	public boolean deleteLog(int log_id) {
		// TODO Auto-generated method stub
		try {
			qr.update("delete from log where id=?", log_id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;
	}
	

}
